package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelItemDao;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-13 14:37
 **/
@Service(interfaceClass = TravelItemService.class)
@Transactional
public class TravelItemServiceImpl implements TravelItemService {

    @Autowired
    TravelItemDao travelItemDao;

    @Override
    public void add(TravelItem travelItem) {
        travelItemDao.add(travelItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        // 利用分页插件来查询
        PageHelper.startPage(currentPage, pageSize);
        Page<TravelItem> page = travelItemDao.findPage(queryString);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public TravelItem findById(Integer id) {
        return travelItemDao.findByid(id);
    }

    @Override
    public void update(TravelItem travelItem) {
        travelItemDao.update(travelItem);
    }

    @Override
    public List<TravelItem> findAll() {
        return travelItemDao.findAll();
    }

    @Override
    public void delete(Integer id) {
        // 这里会有一个错误点，如果自由行模块在外面有主键的话，是不能删除的，我们需要提醒解除关系来进行删除
        Integer count = travelItemDao.findCountByTravelItemItemId(id);
        if (count > 0) {
            throw new RuntimeException("请解除关联关系之后再来删除");
        }
        travelItemDao.delete(id);
    }
}
